Ext.define('SKJF.controller.PENAWARANDET',{
	extend: 'Ext.app.Controller',
	views: ['PENJUALAN.v_penawarandet','PENJUALAN.v_penawarandet_form'],
	models: ['m_penawarandet'],
	stores: ['s_penawarandet'],
	
	requires: ['Ext.ModelManager'],
	
	refs: [{
		ref: 'Listpenawaran',
		selector: 'Listpenawaran'
	}, {
		ref: 'Listpenawarandet',
		selector: 'Listpenawarandet'
	}, {
		ref: 'v_penawarandet_form',
		selector: 'v_penawarandet_form'
	}, {
		ref: 'SaveBtnForm',
		selector: 'v_penawarandet_form #save'
	}, {
		ref: 'CreateBtnForm',
		selector: 'v_penawarandet_form #create'
	}, {
		ref: 'PENAWARANDET',
		selector: 'PENAWARAN #south'
	}],


	init: function(){
		this.control({
			'PENAWARANDET': {
				'afterrender': this.penawarandetAfterRender
			},
			'v_penawarandet_form': {
				'afterlayout': this.formAfterRender
			},
			'Listpenawarandet': {
				'selectionchange': this.enableDelete,
				'itemdblclick': this.updateListpenawarandet
			},
			'Listpenawarandet button[action=create]': {
				click: this.createRecord
			},
			'Listpenawarandet button[action=delete]': {
				click: this.deleteRecord
			},
			'Listpenawarandet button[action=print]': {
				click: this.printRecords
			},
			'v_penawarandet_form button[action=save]': {
				click: this.saveV_penawarandet_form
			},
			'v_penawarandet_form button[action=create]': {
				click: this.saveV_penawarandet_form
			},
			'v_penawarandet_form button[action=cancel]': {
				click: this.cancelV_penawarandet_form
			}
		});
	},
	
	penawarandetAfterRender: function(){
		// var penawarandetStore = this.getListpenawarandet().getStore();
		// penawarandetStore.load();
	},
	
	formAfterRender: function(){
		this.getV_penawarandet_form().down('#TGL_DETAIL_QUOTE_field').focus(false, true);
	},
	
	createRecord: function(){
		var getListpenawarandet	= this.getListpenawarandet();
		var getV_penawarandet_form= this.getV_penawarandet_form(),
			form			= getV_penawarandet_form.getForm();
		var getSaveBtnForm	= this.getSaveBtnForm();
		var getCreateBtnForm	= this.getCreateBtnForm();
		var sel_penawaran = this.getListpenawaran().getSelectionModel().getSelection()[0];
		
		/* grid-panel */
		getListpenawarandet.setDisabled(true);
        
		/* form-panel */
		form.reset();
		getV_penawarandet_form.down('#NOMOR_QUOTE_field').setReadOnly(true);
		getV_penawarandet_form.down('#KODE_BARANG_field').setReadOnly(false);
		
		if(sel_penawaran != null){
			getV_penawarandet_form.down('#NOMOR_QUOTE_field').setValue(sel_penawaran.data.NOMOR_QUOTE);
		}
		
		getSaveBtnForm.setDisabled(true);
		getCreateBtnForm.setDisabled(false);
		getV_penawarandet_form.setDisabled(false);
		
		this.getPENAWARANDET().setActiveTab(getV_penawarandet_form);		
	},
	
	enableDelete: function(dataview, selections){
		this.getListpenawarandet().down('#btndelete').setDisabled(!selections.length);
	},
	
	updateListpenawarandet: function(me, record, item, index, e){
		var getPENAWARANDET		= this.getPENAWARANDET();
		var getListpenawarandet	= this.getListpenawarandet();
		var getV_penawarandet_form= this.getV_penawarandet_form(),
			form			= getV_penawarandet_form.getForm();
		var getSaveBtnForm	= this.getSaveBtnForm();
		var getCreateBtnForm	= this.getCreateBtnForm();
		
		getSaveBtnForm.setDisabled(false);
		getCreateBtnForm.setDisabled(true);
		getV_penawarandet_form.down('#NOMOR_QUOTE_field').setReadOnly(true);
		getV_penawarandet_form.down('#KODE_BARANG_field').setReadOnly(true);
		getV_penawarandet_form.loadRecord(record);
		
		getListpenawarandet.setDisabled(true);
		getV_penawarandet_form.setDisabled(false);
		getPENAWARANDET.setActiveTab(getV_penawarandet_form);
	},
	
	deleteRecord: function(dataview, selections){
		var getstore = this.getListpenawarandet().getStore();
		var selection = this.getListpenawarandet().getSelectionModel().getSelection()[0];
		if(selection){
			Ext.Msg.confirm('Confirmation', 'Are you sure to delete this data: "NOMOR_QUOTE" = "'+selection.data.NOMOR_QUOTE+'","KODE_BARANG" = "'+selection.data.KODE_BARANG+'","ID" = "'+selection.data.ID+'"?', function(btn){
				if (btn == 'yes'){
					getstore.remove(selection);
					getstore.sync();
				}
			});
			
		}
	},
	
	printRecords: function(){
		var getstore = this.getListpenawarandet().getStore();
		var jsonData = Ext.encode(Ext.pluck(getstore.data.items, 'data'));
		
		Ext.Ajax.request({
			method: 'POST',
			url: 'c_penawarandet/printRecords',
			params: {data: jsonData},
			success: function(response){
				var result=eval(response.responseText);
				switch(result){
				case 1:
					win = window.open('./temp/penawarandet.html','penawarandet_list','height=400,width=900,resizable=1,scrollbars=1, menubar=1');
					break;
				default:
					Ext.MessageBox.show({
						title: 'Warning',
						msg: 'Unable to print the grid!',
						buttons: Ext.MessageBox.OK,
						animEl: 'save',
						icon: Ext.MessageBox.WARNING
					});
					break;
				}  
			}
		});
	},
	
	saveV_penawarandet_form: function(){
		var getPENAWARANDET		= this.getPENAWARANDET();
		var getListpenawarandet 	= this.getListpenawarandet();
		var getV_penawarandet_form= this.getV_penawarandet_form(),
			form			= getV_penawarandet_form.getForm(),
			values			= getV_penawarandet_form.getValues();
		var store 			= this.getStore('s_penawarandet');
		
		if (form.isValid()) {
			var jsonData = Ext.encode(values);
			
			Ext.Ajax.request({
				method: 'POST',
				url: 'c_penawarandet/save',
				params: {data: jsonData},
				success: function(response){
					store.reload({
						callback: function(){
							var newRecordIndex = store.findBy(
								function(record, id) {
									if (record.get('NOMOR_QUOTE') === values.NOMOR_QUOTE && record.get('KODE_BARANG') === values.KODE_BARANG && record.get('ID') === values.ID) {
										return true;
									}
									return false;
								}
							);
							/* getListpenawarandet.getView().select(recordIndex); */
							getListpenawarandet.getSelectionModel().select(newRecordIndex);
						}
					});
					
					getV_penawarandet_form.setDisabled(true);
					getListpenawarandet.setDisabled(false);
					getPENAWARANDET.setActiveTab(getListpenawarandet);
				}
			});
		}
	},
	
	createV_penawarandet_form: function(){
		var getPENAWARANDET		= this.getPENAWARANDET();
		var getListpenawarandet 	= this.getListpenawarandet();
		var getV_penawarandet_form= this.getV_penawarandet_form(),
			form			= getV_penawarandet_form.getForm(),
			values			= getV_penawarandet_form.getValues();
		var store 			= this.getStore('s_penawarandet');
		
		if (form.isValid()) {
			var jsonData = Ext.encode(values);
			
			Ext.Ajax.request({
				method: 'POST',
				url: 'c_penawarandet/save',
				params: {data: jsonData},
				success: function(response){
					store.reload();
					
					getV_penawarandet_form.setDisabled(true);
					getListpenawarandet.setDisabled(false);
					getPENAWARANDET.setActiveTab(getListpenawarandet);
				}
			});
		}
	},
	
	cancelV_penawarandet_form: function(){
		var getPENAWARANDET		= this.getPENAWARANDET();
		var getListpenawarandet	= this.getListpenawarandet();
		var getV_penawarandet_form= this.getV_penawarandet_form(),
			form			= getV_penawarandet_form.getForm();
			
		form.reset();
		getV_penawarandet_form.setDisabled(true);
		getListpenawarandet.setDisabled(false);
		getPENAWARANDET.setActiveTab(getListpenawarandet);
	}
	
});